//
//  DGNListCollectionCell.swift
//  Digimon
//
//  Created by Vill on 2023/10/13.
//

import UIKit
import Kingfisher

class DGNListCollectionCell: UICollectionViewCell {
    
    // 在线状态
    @IBOutlet weak var statusImg: UIImageView!
    // 头像
    @IBOutlet weak var iconImg: UIImageView!
    // 底部view
    @IBOutlet weak var bottomContain: UIView!
    // 昵称
    @IBOutlet weak var nameLbl: UILabel!
    // 国家
    @IBOutlet weak var countryLbl: UILabel!
    // 价格
    @IBOutlet weak var priceLbl: UILabel!
    // 视频按钮
    @IBOutlet weak var videoBtn: UIButton!
    
    var model:HotListModel?{
        didSet {
            guard let model = model else {
                return
            }
            if model.coverUrl.isEmptyStr{
                self.iconImg.kf.setImage(with: KUrlImaged(model.profilePhoto), placeholder: KImaged("coco_pic_holder"), options: [.transition(.fade(0.4))], completionHandler: nil)
            }else{
                self.iconImg.kf.setImage(with: KUrlImaged(model.coverUrl), placeholder: KImaged("coco_pic_holder"), options: [.transition(.fade(0.4))], completionHandler: nil)
            }
            
            nameLbl.text = model.name
            //是否在线，1在线、2繁忙2繁忙,3下线
            if model.status == 1{
                self.statusImg.image = KImaged("coco_status_online")
                self.videoBtn.setImage(KImaged("coco_video_online"), for: .normal)
            }else if model.status == 2{
                self.statusImg.image = KImaged("coco_status_busy")
                self.videoBtn.setImage(KImaged("coco_video_busy"), for: .normal)
            }else if model.status == 3{
                self.statusImg.image = KImaged("coco_status_offline")
                self.videoBtn.setImage(KImaged("coco_video_offline"), for: .normal)
            }
            self.priceLbl.attributedText = "\(model.price)".stringWithImage(img: "coco_mine_star", bounds: CGRect(x: 0, y: -PTW(2) , width: PTW(12) , height: PTW(12)),isFirst: true)
            
            var countrylogo = ""
            let country = !model.country.isEmptyStr ? model.country:"United States of America"
            for i in 0..<model.countryCodeArr.count {
                let countryCode = model.countryCodeArr[i]
                if (countryCode.enName == country){
                    countrylogo = String.emojiFlagForISOCountryCode(countryCode.abbreviate)()
                    break
                }else{
                    countrylogo = country
                }
            }
            if (model.age == 0){
                self.countryLbl.text = country
            }else{
                self.countryLbl.text = countrylogo+",\(model.age)"
            }
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        self.contentView.layer.cornerRadius = PTW(20)
        self.contentView.layer.masksToBounds = true
        DispatchQueue.main.async {
            self.bottomContain.setGradientBgColor(colors: [UIColor.colorWithHex("#000000", 0),UIColor.colorWithHex("#000000",0.2)])
        }
    }

    // 拨打
    @IBAction func videoAction(_ sender: UIButton) {
        self.viewController()?.requestStatus(userId: self.model?.id ?? "")
    }
    

}
